﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;

public partial class Popup_Popup_Mat_Add_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetCombo();

            if (!string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                this.hdCode.Text = Request.QueryString["code"];
                SetMat();
            }
        }

        if (!X.IsAjaxRequest)
        {
            //분석 요
            string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. Maecenas tortor turpis, interdum non, sodales non, iaculis ac, lacus. Vestibulum auctor, tortor quis iaculis malesuada, libero lectus bibendum purus, sit amet tincidunt quam turpis vel lacus. In pellentesque nisl non sem. Suspendisse nunc sem, pretium eget, cursus a, fringilla vel, urna.";

            this.ResourceManager1.RegisterClientScriptBlock("text", string.Format("var text=\"{0}\";", text));

            foreach (Portlet portlet in Ext.Net.Utilities.ControlUtils.FindControls<Portlet>(this.Page))
            {
                if (!portlet.ID.Equals("Portlet1"))
                {
                    portlet.Html = "={text}";
                    portlet.Padding = 5;
                }
            }

        }
    }

    private void SetMat()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_D_MA_DB_S2(this.hdCode.Text);

            this.tbxName.Text = ds.Tables[0].Rows[0]["NAME"].ToString();
            this.tbxSpec.Text = ds.Tables[0].Rows[0]["SPEC"].ToString();
            this.tbxUnit.Text = ds.Tables[0].Rows[0]["UNIT"].ToString();
            this.tbxUc.Text = ds.Tables[0].Rows[0]["UC"].ToString();
            this.tbxRemark.Text = ds.Tables[0].Rows[0]["REMARK"].ToString();
        }
    }

    private void SetCombo()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_SP_DDDW_MCLASS_LEVEL1_S();
            StrClass1.DataSource = ds.Tables[0];
            StrClass1.DataBind();

            this.cbxUpClass.SelectedIndex = 0;

            ds = oService.Select_SP_DDDW_MCLASS_LEVEL2_S("");
            StrClass2.DataSource = ds.Tables[0];
            StrClass2.DataBind();

            this.cbxClass.SelectedIndex = 0;
        }
    }

    protected void ChangeGubun(object sender, DirectEventArgs e)
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            StrClass2.RemoveAll();

            DataSet ds = oService.Select_SP_DDDW_MCLASS_LEVEL2_S(this.cbxUpClass.SelectedItem.Value);
            StrClass2.DataSource = ds.Tables[0];
            StrClass2.DataBind();

            this.cbxClass.SelectedIndex = 0;
        }
    }

    protected void btnSave_Click(object sender, DirectEventArgs e)
    {
        SaveMatMaster();
    }


    protected void SaveMatMaster()
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                int iResult;
                if (string.IsNullOrEmpty(this.hdCode.Text))
                {
                    DateTime dt = new DateTime();
                    dt = DateTime.Now;
                    string ym = dt.Year.ToString() + dt.Month.ToString();
                    iResult = oService.INSERT_D_MA_DB_I(ym, this.tbxName.Text, this.tbxSpec.Text, this.tbxUnit.Text, this.tbxUc.Text, this.cbxClass.SelectedItem.Value, Session["JUMIN_NM"].ToString(), this.tbxRemark.Text);
                }
                else
                {
                    iResult = oService.UPDATE_D_MA_DB_U(this.hdCode.Text, this.tbxName.Text, this.tbxSpec.Text, this.tbxUnit.Text, this.tbxUc.Text, Session["JUMIN_NM"].ToString(), this.tbxRemark.Text);
                }
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
}
